package cn.hzking.gjmall.activitys.second;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.adpters.home.SenckillAdapter;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.SkillBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.GjCustomStateViewObserver;
import cn.hzking.gjmall.utils.SwipeRefreshLayoutUtils;
import cn.hzking.gjmall.view.SimpleOptionView;

/**
 * 秒杀列表
 */

public class SeckillGoodListActivity extends ColorStatuBarActivity {
    private  final  String  CATEGORYID = "secillActivityId";

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;
    @Bind(R.id.refrsh)
    SwipeRefreshLayout refrsh;
    @Bind(R.id.statlayout)
    StateLayout statlayout;
    private String msId;
    private SenckillAdapter senckillAdapter;
    private int currentPage = 1;
    private int secillActivityId = 0; //秒杀的id
    private int itemSkuId = 0;
    private int number = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seckill_good_list);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        intent.getStringExtra("secillActivityId");
        msId = intent.getStringExtra(CATEGORYID);
      //  categoryId = String.valueOf(60);
        SwipeRefreshLayoutUtils.initRefreshLayout(refrsh);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);
        senckillAdapter = new SenckillAdapter(R.layout.item_goodlist, null);
        senckillAdapter.setMsId(msId);
        recyclerview.setAdapter(senckillAdapter);
        initListener();
        getNetData(true, false, true, true, null, null, null);

    }

    private void initListener() {
        refrsh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getNetData(true, true, false, true, null, null, null);
            }
        });
    }

    public void getNetData( boolean isFristInit, boolean isRefrsh, boolean isShowDialog, boolean isCancel, View emptyView, View onNetView, View errorView) {
        HashMap<String, String> parms = new HashMap<>();
        parms.put("secillActivityId",String.valueOf(secillActivityId));
        RetrofitFactory.getInstant().API()
                .getSeriverSeckillData(parms)
                .compose(this.<SkillBean>setThread())
                .subscribe(new GjCustomStateViewObserver<SkillBean>(this, statlayout,
                        refrsh, isFristInit, isRefrsh, isShowDialog, isCancel, emptyView, onNetView, errorView) {
                    @Override
                    protected void onBusinessSuccess(SkillBean categoryListBean) throws Exception {
                        if (categoryListBean != null) {
                            simpleTileView.setTitle(String.valueOf(categoryListBean.getData().getSecillTitle()));
                            List<SkillBean.DataBean.SecillListBean> itemList = categoryListBean.getData().getSecillList();
                            if (currentPage == 1) {
                                senckillAdapter.setNewData(itemList);
                                if (senckillAdapter.getData().size() == 0) {
                                    View view = View.inflate(mActivity,R.layout.layout_statelayout_custom_goodlist_empty,null);
                                    statlayout.showCustomView(view);

                                    return;
                                }
                            } else {
                                senckillAdapter.addData(itemList);
                            }
                            currentPage++;
                        }
                        int itemCount = senckillAdapter.getData().size();
                     /*   if (categoryListBean.getData().getTotalRecord() == itemCount) {//数据加载完成
                            cateGoryGoodListAdapter.loadMoreEnd(true);

                        } else {
                            cateGoryGoodListAdapter.loadMoreComplete();
                        }*/

                    }

                    @Override
                    protected void onChildFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }


                });
    }


}
